package com.ks.system.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ks.handler.BusinessException;
import com.ks.response.ResultCode;
import com.ks.system.entity.Log;
import com.ks.system.mapper.LogMapper;
import com.ks.system.service.LogService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ks.system.vo.LogVO;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 操作日志表 服务实现类
 * </p>
 *
 * @author ks
 * @since 2020-12-01
 */
@Service
public class LogServiceImpl extends ServiceImpl<LogMapper, Log> implements LogService {


    @Override
    public void saveLog(Log log) {
        this.baseMapper.insert(log);
    }

    @Override
    public Integer delete(Long id) {
        Log log = this.baseMapper.selectById(id);
        //判断日志记录是否存在
        if (log != null) {
            throw new BusinessException(ResultCode.LOG_NOT_EXIST.getCode(),ResultCode.LOG_NOT_EXIST.getMessage());
        }
        return this.baseMapper.deleteById(id);
    }

    @Override
    public Page<Log> findLogList(Page<Log> page, LogVO logVO) {
        List<Log> logList = this.baseMapper.findLogList(page, logVO);
        return page.setRecords(logList);
    }

    @Override
    public void batchDelete(List<Long> list) {
        list.forEach(log ->{
            Log byId = this.baseMapper.selectById(log);
            if (byId == null) {
                throw new BusinessException(ResultCode.LOG_NOT_EXIST.getCode(),ResultCode.LOG_NOT_EXIST.getMessage());
            }
            this.baseMapper.deleteById(log);
        });
    }
}
